package IOdemo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import org.junit.Test;

public class IOTest {
	@Test
	public void IOtest() {
		try {
			FileReader fileReader = new FileReader("IO.txt");
			int ch = -1;
			while ((ch = fileReader.read()) != -1){
				System.out.println((char)ch);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	@Test
	public void IOTest01() {
		FileReader fileReader =  null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("io.txt");
			fileWriter = new FileWriter("io_b.txt");
			char[] buffer = new char[10];
			int length = 0;
			while((length = fileReader.read(buffer)) != -1){
				fileWriter.write(buffer,0,length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	@Test
	public void IOTest02() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream("1.jpeg");
			fileOutputStream = new FileOutputStream("1_b.jpeg");
			byte[] buffer = new byte[1024];
			int length = 0;
			while((length = fileInputStream.read(buffer)) != -1){
				fileOutputStream.write(buffer, 0, length);
			}
			
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

	@Test
	public void IOTest03() {
		Student student = new Student(1, "zjx", "nan", "ui");
		ObjectOutputStream objectOutputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream("person");
			objectOutputStream = new ObjectOutputStream(fileOutputStream);
			objectOutputStream.writeObject(student);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (objectOutputStream != null) {
				try {
					objectOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
	@Test
	public void IOTest04() {
		InputStream inputStream = null;
		ObjectInputStream objectInputStream  = null;
		try {
			inputStream = new FileInputStream("person");
			objectInputStream  = new ObjectInputStream(inputStream);
			Object object = objectInputStream.readObject();
			Student student  = (Student) object;
			System.out.println(student);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
